//
//  ColumnView.m
//  HimalayaMusic
//
//  Created by dllo on 16/1/6.
//  Copyright © 2016年 WXCLegendary. All rights reserved.
//

#import "ColumnView.h"

@implementation ColumnView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.array = @[@"推荐", @"分类", @"榜单", @"主播"];
        self.buttonArray = [NSMutableArray array];
        self.scrollLabel = [[UILabel alloc] init];
        self.scrollLabel.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.scrollLabel];
        [self setButton];
        
        
    }
    return self;
}

- (void)setButton {
    for (int i = 0; i < self.array.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        button.frame = CGRectMake(i * self.width / 4 , 0, self.width / 4, self.height);
        [button setTitle:self.array[i] forState:UIControlStateNormal];
        [self addSubview:button];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.buttonArray addObject:button];
        button.tag = 100 + i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UIButton *button = self.buttonArray[0];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:18];
    self.scrollLabel.frame = CGRectMake(0, self.height - 2, button.width, 2);
}

- (void)buttonAction:(UIButton *)sender {
    self.collectionView.contentOffset = CGPointMake((sender.tag - 100) * self.width, 0);
    for (UIButton *button in self.buttonArray) {
        if (button.tag == sender.tag) {
            [sender setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            sender.titleLabel.font = [UIFont systemFontOfSize:18];

//Label移动动画
            [UIView animateWithDuration:1.0 animations:^{
                //动画重复次数
                [UIView setAnimationRepeatCount:1];
                //设置动画变化的曲线(1.前后都慢)(2.前快后慢)(3.前慢后快)(4.匀速)
                [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
                //动画延迟几秒播放
                [UIView setAnimationDelay:0];
              self.scrollLabel.origin = CGPointMake(sender.left, self.height - 2);
            }];
        } else {
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:15];
        }
    }
}



@end
